#include "taskQueue.h"
int taskQueueInit(taskQueue_t *pqueue) {
    if(pqueue == NULL) {
        printf("taskqueue not init\n");
        return -1; // 错误：传入的队列指针为空
    }

    bzero(pqueue, sizeof(taskQueue_t));

    return 0;
}

int enQueue(taskQueue_t *pqueue, int netfd) {
    task_node_t *pNew = (task_node_t *)calloc(1, sizeof(task_node_t));
    pNew->netfd = netfd;

    if (pqueue->queueSize == 0) {
        pqueue->head = pNew;
        pqueue->tail = pNew;
    } else {
        pqueue->tail->next = pNew;
        pqueue->tail = pNew;
    }
    ++pqueue->queueSize;
    return 0;
}


int deQueue(taskQueue_t *pqueue) {
    if (pqueue->queueSize == 0) {
        return -1; // 队列为空，无法出队
    }

    task_node_t *pNew = pqueue->head;
    pqueue->head = pqueue->head->next;
    if (pqueue->queueSize == 1) {
        pqueue->tail = NULL;
    }
    free(pNew);
    pqueue->queueSize--;
    return 0;
}

int printQueue(taskQueue_t *pqueue){
    task_node_t * pCur = pqueue->head;
    while(pCur){
        printf("%3d", pCur->netfd);
        pCur = pCur->next;
    }
    printf("\n");
    return 0;
}